<?php
	session_start();
	$title_page = 'Shop Follow';
	include '../includes/html_templates/header.php';
	include ($_SERVER['DOCUMENT_ROOT']."/wewant1/includes/Header/redirectTo.php");
	include ($_SERVER['DOCUMENT_ROOT']."/wewant1/includes/db_exe/db_connection.php")
?>

<?php
	if(isset($_SESSION['id_user'])){
		//kiem tra neu user da dang nhap thi thuc hien tiep follow shp
		$db = connectDb();

		//lay het tat ca cac shop ra
		//them truong id follow cho nhung shop da duoc user nay follow
		$q = "SELECT s.id_shop, s.shop_url, f2.id_follow";
		$q .= " FROM shops AS s";
		//left join voi bang f2 tao boi query ben duoi
		$q .= " LEFT JOIN";
		//tao bang f2 chua tat ca follow cua user nay doi voi tat cac cac Shop (chi lay shop thoi khong lay follow user)
		$q .= " (SELECT f.id_follow, f.id_followed FROM follows AS f WHERE (f.id_user_follow = 1) AND (f.shop_or_user_followed = 0)) as f2";
		$q .= " ON (s.id_shop = f2.id_followed)";
		$q .= " ORDER BY s.id_shop ASC";

		echo $q;
		$r = exeQuery($db,$q);

		$idAllShop =  array();
		while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){

			$idAllShop[] = array($row['id_shop'],$row['shop_url'],$row['id_follow']);
		}
	foreach ($idAllShop as list($idShop,$shopUrl,$idFollow)) {

			$q = "SELECT id_product,image_src FROM products WHERE id_shop = $idShop LIMIT 0,3";
			$r = exeQuery($db,$q);

			echo "<div style='margin: 100px;'>";

			echo "<p><a href='shop.php?idshop={$idShop}'>$shopUrl</a></p>";
			//hien ra url cua shop

			if(is_null($idFollow)){
				//kiem tra dieu kien user da theo doi shop do chua, neu co hien ra da theo doi, chua hien ra chua
				echo "<a href='' class='followBtn' data-id-shop='{$idShop}' resource='Shop' data-status-follow='follow'>Theo dõi</a>";
			} else {
				echo "<a href='' class='followBtn' data-id-shop='{$idShop}' resource='Shop' data-status-follow='following'>Đã theo dõi</a>";
			}//ket thu kiem tra dieu kien user da theo doi shop do chua, neu co hien ra da theo doi, chua hien ra chua
			
			while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) {
					// echo $row['id_product'].'<br />';
					echo "<a href='product.php?id={$row['id_product']}' >";
					echo "<img src='../upload/image_product/x200-{$row['image_src']}.jpg' style='margin: 20px; width: 120px; height:120px;' />";
					echo "</a>";
			}
			echo "</div>";
		}	
		

	} else {
		redirectTo('trending.php');
	}
?>
<!-- ajax cho follow button, gui request va thay doi theo doi thanh da theo doi -->
<script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $(".followBtn").click(function(){
      	var foButton = $(this);
      	var idShop = $(this).attr('data-id-shop');
      	var resource = $(this).attr('resource');

      	if(!isNaN(idShop)) {
      	
          $.post( 
             "/wewant1/sites/ajax/followShop.php",
             'idShop='+idShop+'&resource='+resource,
             function(data) {
             	if(data == 1){
             		foButton.text("Đã theo dõi");//tra ve 1 la insert thanh cong follows
             	} else if(data == 0){
             		foButton.text("Theo dõi");//ket qua tra ve la 0 tuc la da delete follows
             	}
             }
          );//end post
      	}//end if check co phai la dang so khong

    	return false;
      });
   });
</script>
</body>
</html>